package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// Material 素材表
type Material struct {
	ID             string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	Name           string `json:"name" gorm:"column:name;type:string;size:50;not null;default:'';comment:素材名称"`
	Type           int8   `json:"type" gorm:"column:type;type:tinyint(1);not null;default:1;comment:素材类型,1图片,2视频"`
	FolderID       string `json:"folder_id" gorm:"column:folder_id;type:string;size:30;not null;default:'0';comment:分组id"`
	Position       int8   `json:"position" gorm:"column:position;type:tinyint(1);not null;default:1;comment:所属位置,1前屏,2后屏"`
	Duration       int64  `json:"duration" gorm:"column:duration;type:int;size:64;not null;default:0;comment:视频时长"`
	Suffix         string `json:"suffix" gorm:"column:suffix;type:string;size:30;not null;default:'';comment:素材格式"`
	Size           string `json:"size" gorm:"column:size;type:string;size:30;not null;default:'';comment:素材大小"`
	Status         int8   `json:"status" gorm:"column:status;type:tinyint(1);not null;default:1;comment:状态:1等待审核,2审核通过可用,3禁用,4审核未通过"`
	FilePath       string `json:"file_path" gorm:"column:file_path;type:text;not null;comment:文件路径"`
	FileSize       int64  `json:"file_size" gorm:"column:file_size;type:int;size:64;not null;default:0;comment:素材大小"`
	CheckMd5       string `json:"check_md5" gorm:"column:check_md5;type:varchar(32);not null;default:'';comment:文件md5"`
	MerchantID     string `json:"merchant_id" gorm:"column:merchant_id;primaryKey;type:varchar(30);not null;default:'';comment:商户id"`
	MerchantName   string `json:"merchant_name" gorm:"column:merchant_name;primaryKey;type:varchar(50);not null;default:'';comment:商户名称"`
	CreateUser     string `json:"create_user" gorm:"column:create_user;primaryKey;type:varchar(30);not null;default:'';comment:用户id"`
	CreateUserName string `json:"create_username" gorm:"column:create_username;primaryKey;type:varchar(50);not null;default:'';comment:用户名称"`
	CreatedAt      int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;not null;default:0;index:created_at;comment:创建时间"`
	DeletedAt      int64  `json:"deleted_at" gorm:"column:deleted_at;type:int;size:64;not null;default:0;comment:删除时间"`
}

// TableName 表名
func (Material) TableName() string {
	return "material"
}

// BeforeCreate 自增id
func (l *Material) BeforeCreate(tx *gorm.DB) (err error) {
	if l.ID == "" {
		// id为空时，自动添加id
		uuid, err := helper.UUID{}.String()
		if err != nil {
			return err
		}
		l.ID = uuid
	}
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &Material{})
}
